#!/usr/bin/python3
# ******************************************************************************
# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
# licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# ******************************************************************************/
"""
Description: release method's entrance for custom commands
Class:ReleaseCommand
"""
import os

import jenkins

from javcra.api.jenkins_api import JenkinsJob
from javcra.application.checkpart.checktest import CheckTest
from javcra.application.modifypart.modifyentrance import IssueOperation
from javcra.cli.base import BaseCommand
from javcra.application.serialize.validate import validate_giteeid
from javcra.application.releasepart.releaseentrance import ReleaseEntrance
from javcra.common import constant
from javcra.common.constant import GITEE_TOKEN


class ReleaseCommand(BaseCommand):
    """
    Description: start the release part
    Attributes:
        sub_parse: Subcommand parameters
        params: Command line parameters
    """

    def __init__(self):
        """
        Description: Instance initialization
        """
        super(ReleaseCommand, self).__init__()
        self.sub_parse = BaseCommand.subparsers.add_parser(
            'release', help="release assistant of release part")

        self.add_issueid_arg()
        self.add_giteeid_arg()
        self.sub_parse.add_argument(
            '--type',
            help='Specify the release check type, only allow checkok and cvrfok',
            action='store',
            choices=['checkok', 'cvrfok']
        )

    def do_command(self, params):
        """
        Description: Executing command
        Args:
            params: Command line parameters
        Returns:

        Raises:

        """
        issue_id = params.releaseIssueID
        gitee_id = params.giteeid

        release_type = params.type
        comment = "/" + release_type[:-2] + "-" + release_type[-2:]

        print("release part", issue_id, gitee_id)
        _repo = "pkgship"

        s = IssueOperation(_repo, GITEE_TOKEN, issue_id)
        check = CheckTest(_repo, GITEE_TOKEN, params.releaseIssueID)
        personnel_authority = check.parsing_body()
        permission = validate_giteeid(gitee_id, comment, personnel_authority)
        if not permission:
            return

        print("通过校验，开始发布")



        # # 获取pkg_list后， 传到测试服务器中
        #
        # # 创建jenkins实例所需参数
        # base_url = 'https://jenkins.openeuler.org'
        # jenkins_user = 'tuShenmei'
        # jenkins_passwd = '062725tu_TU'
        # paral_num = 5
        # install_build_pkgs = ["bluez", "edk2", "httpd", "jetty", "libvma"]
        # branch = "openEuler-20.03-LTS-SP1"
        # update_time = "20210630"
        # jenkins_server = JenkinsJob(base_url, jenkins_user, jenkins_passwd, paral_num, install_build_pkgs,
        #                             branch, update_time)
        #
        # # 传到测试服务器中所需参数
        # job_name = "obs/guoxiaoqi_test"
        # # 通过s.get_update_list()获取
        # obs_pkgs = ["dbus", "dbus-cpp", "dbus-glib", "dbus-python"]
        #
        # params = {"ScanOSSAPIURL": constant.JENKINS_API_URL,
        #           "ScanOSSResultRepo": constant.JENKINS_SERVER_REPO,
        #           "action": "create",
        #           "obs_project": "openEuler:20.09",
        #           "update_dir": "update_20210630",
        #           "package_family": "standard",
        #           "pkgnamelist": ",".join(obs_pkgs)
        #           }
        # jenkins_server.build_jenkins_job(job_name, params)


